package com.hhkj.admin.course.common.component.checker;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.domain.CourseBankType;
import com.hhkj.admin.course.service.base.CourseBankTypeService;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * CourseBankTypeChecker
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/13 16:12
 */
@Component
public class CourseBankTypeChecker
{

    @Autowired
    private CourseBankTypeService service;

    /**
     * 校验名称是否重复
     *
     * @param courseBankType 课程题库类型信息
     */
    public void checkNameRepeat(CourseBankType courseBankType)
    {
        // 2、类型名称是否重复
        LambdaQueryWrapper<CourseBankType> typeQuery = new LambdaQueryWrapper<>();
        typeQuery.eq(CourseBankType::getCourseId, courseBankType.getCourseId())
                .eq(ObjectUtil.isNotNull(courseBankType.getId()), CourseBankType::getId, courseBankType.getId())
                .eq(CourseBankType::getName, courseBankType.getName());
        long nameRepeatAmount = service.count(typeQuery);
        if (nameRepeatAmount > 0) {
            throw new GlobalException("名称重复");
        }
    }
}
